<?php

function fetch($query)
{
	return mysql_fetch_array(mysql_query($query));
}

function makepassword($string = "")
{
	$letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	if ($string)
	{
		$num = rand(10,99);
		$string .= $num;
		$x = 0;
		while ($x < 2)
		{
			$which_letter = rand(0,25);
			$string .= $letters[$which_letter];
			$x++;
		}
	}
	if (!$string)
	{
		$x = 0;
		while ($x < 6)
		{
			$which_letter = rand(0,25);
			$string .= $letters[$which_letter];
			$x++;
		}
		$num = rand(1000,9999);
		$string .= $num;
	}
	return $string;
}

function smilies($string)
{
	include "config.inc.php";
	$smilies = file("$websiteURL/lists/smilies.txt");
	$num_smilies = count($smilies);
	$x = 0;
	while ($x < $num_smilies)
	{
		$array = explode(" ",$smilies[$x]);
		$array[3] = ereg_replace("\n","",$array[3]);
		$string = eregi_replace($array[0],"<img src=images/smilies/$array[1] height=$array[2] width=$array[3]>",$string);
		$x++;
	}
	return $string;
}

function unsmilies($string)
{
	include "config.inc.php";
	$smilies = file("$websiteURL/lists/smilies.txt");
	$num_smilies = count($smilies);
	$x = 0;
	while ($x < $num_smilies)
	{
		$array = explode(" ",$smilies[$x]);
		$array[3] = ereg_replace("\n","",$array[3]);
		$string = eregi_replace("<img src=images/smilies/$array[1] height=$array[2] width=$array[3]>",stripslashes($array[0]),$string);
		$x++;
	}
	return $string;
}

function badwords($string)
{
	include "config.inc.php";
	$badwords = file("$websiteURL/lists/badwords.txt");
	$num_badwords = count($badwords);
	$x = 0;
	$y = 0;
	$z = 0;
	while (($x < $num_badwords) AND ($y != 1))
	{
		$badword = explode(" ",$badwords[$x]);
		$badword[1] = ereg_replace("\n","",$badword[1]);
		$string = eregi_replace($badword[0],$badword[1],$string);
		if (!$badword[1])
		{
			$y = 1;
		}
		$x++;
	}
	$words = explode(" ",$string);
	$num_words = count($words);
	while ($x < $num_badwords)
	{
		$badword = explode(" ",$badwords[$x]);
		while ($z < $num_words)
		{
			if ($words[$z] == $badword[0])
			{
				$words[$z] = ereg_replace("\n","",$badword[1]);
			}
			$z++;
		}
		$x++;
	}
	$string = implode(" ",$words);
	return $string;
}

function format($string,$textColor)
{

	$string = ereg_replace("<a href=","<a target=_blank href=", $string);

	$string = ereg_replace("\r","\n",$string);
	$string = ereg_replace("\n\n","</font></p><p><font color=$textColor>",$string);
	$string = ereg_replace("\n","<br>",$string);
	$string = "<p><font color=$textColor>$string</font></p>";
	return $string;
}

function unformat($string)
{
	$string = ereg_replace("</p><p>","\n\r\n\r",$string);
	$string = ereg_replace("<br>","\n\r",$string);
	$string = ereg_replace("<p>","",$string);
	$string = ereg_replace("</p>","",$string);
	return $string;
}

function space2plus($string)
{
	$string = ereg_replace(" ","+",$string);
	return $string;
}

function check_length($string,$int)
{
	$num_chars = strlen($string);
	if ($num_chars <= $int)
	{
		$check = 1;
	}
	else
	{
		$check = 0;
	}
	return $check;
}

function alternate($var,$string1,$string2)
{
	if ($var == $string1)
	{
		$string = $string2;
	}
	if (($var == $string2) or (!$var))
	{
		$string = $string1;
	}
	return $string;
}

function error($page,$error = "")
{
	include "globals.inc.php";
	if (ereg("\?",$page))
	{
		$string = "Location: $page&error=" . space2plus($error);
	}
	else
	{
		$string = "Location: $page?error=" . space2plus($error);
	}
	return $string;
}

function systemmessage($game,$page,$user,$message)
{
	$timestamp = date("U");
	mysql_query("INSERT INTO messages2 (userid,message,link,game,message_time) VALUES ('$user','$message','$page','$game','$timestamp')");
	return 1;
}

function points($game,$user_to_award,$award_this_many)
{
	$findAwardTo = fetch("SELECT points FROM members2 WHERE username = '$user_to_award' AND game = '$game'");
	mysql_query("UPDATE members2 SET points=$findAwardTo[points]+$award_this_many WHERE username = '$user_to_award' AND game = '$game'");
	return 1;
}

function quote($string)
{
	$string = eregi_replace("\(quote\)", "<blockquote><font size=-1><i>", $string);
	$string = eregi_replace("\(endquote\)", "</i></font></blockquote>", $string);
	return $string;
}

function unquote($string)
{
	$string = eregi_replace("<blockquote><b><font size=-1>", "\(quote\)", $string);
	$string = eregi_replace("</font></blockquote></i>", "\(endquote\)", $string);
	return $string;
}

function stripspaces($string)
{
	$string = ereg_replace(" ","",$string);
	return $string;
}

function record($type, $entry, $timestamp, $gameid, $userid = 0)
{
	mysql_query("INSERT INTO records VALUES ('', '$type', '$entry', '$userid', '$gameid', '$timestamp')");
	return 1;
}
?>